package top.xia17.plugins.fast.user.rest;

import top.xia17.plugins.fast.common.model.Back;
import top.xia17.plugins.fast.common.model.PageData;
import top.xia17.plugins.fast.common.model.PageParam;
import top.xia17.plugins.fast.common.database.jpa.JpaUtil;
import top.xia17.plugins.fast.common.exceptions.BadRequestException;
import top.xia17.plugins.fast.common.utils.VerifyUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import top.xia17.plugins.fast.user.dao.entity.User;
import top.xia17.plugins.fast.user.model.dto.UserDto;
import top.xia17.plugins.fast.user.model.query.UserQuery;
import top.xia17.plugins.fast.user.model.req.SetUserRolesReq;
import top.xia17.plugins.fast.user.service.UserService;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import java.util.List;

/**
 * 用户rest
 * @author xia17
 * @date 2022/1/17
 */
@RestController
@RequestMapping("user/mgmt")
@RequiredArgsConstructor
public class UserMgmtRest {

    private final UserService userService;


    /**
     * 新增用户
     * @param resource /
     * @return /
     */
    @PostMapping("create")
    public Back<UserDto> create(@RequestBody @Valid User resource){
        UserDto user = userService.createDto(resource);
        return Back.ok(user);
    }


    /**
     * 修改用户
     * @param resource /
     * @return /
     */
    @PostMapping("update")
    public Back<UserDto> update(@RequestBody @Valid User resource) {
        UserDto user = userService.updateDto(resource);
        return Back.ok(user);
    }

    /**
     * 删除用户
     *
     * @param id 用户id
     * @return /
     */
    @PostMapping("delete")
    public Back<String> delete(@NotBlank Long id) {
        VerifyUtil.isLongId(id, "参数异常");
        userService.deleteById(id);
        return Back.ok();
    }

    /**
     * 删除多个用户
     * @param ids 用户IDS
     * @return /
     */
    @PostMapping("delete_by_ids")
    public Back<String> deleteByIds(@RequestBody List<Long> ids){
        if (ids.isEmpty()){
            throw new BadRequestException("请至少传一个用户ID");
        }
        userService.deleteById(ids);
        return Back.ok();
    }


    /**
     * 用户分页列表
     *
     * @param userQuery 查询条件
     * @param pageParam 分页参数
     * @return /
     */
    @GetMapping
    public Back<PageData<UserDto>> page(UserQuery userQuery, @Valid PageParam pageParam) {
        Page<UserDto> page = userService.findAllDto(userQuery, pageParam.toPageable());
        return Back.ok(JpaUtil.page(page));
    }

    /**
     * 用户列表，不分页
     * @param userQuery /
     * @return /
     */
    @GetMapping("list")
    public Back<List<UserDto>> list(UserQuery userQuery) {
        List<UserDto> list = userService.findAllDto(userQuery);
        return Back.ok(list);
    }

    /**
     * 设置用户的角色
     *
     * @param req 请求信息
     * @return /
     */
    @PostMapping("set_user_roles")
    public Back<UserDto> setUserRoles(@RequestBody @Valid SetUserRolesReq req) {
        UserDto user = userService.setUserRoles(req.getId(), req.getRoleIds());
        return Back.ok(user);
    }


}
